<?php

namespace app\service;


use app\common\Esclient;
use app\common\model\AnchorLevelRuleModel;
use app\common\model\UserLevelRuleModel;
use app\common\model\UserModel;

class UserService extends Service
{
    // esdb es_anchor索引
    static protected $es_anchor = 'es_anchor';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 添加主播es数据
     */
    static public function setAnchorEs($anchor_id)
    {
        // 查询主播信息
        $field       = 'id,nick_name,avatar,is_anchor,anchor_point,point';
        $anchor_info = UserModel::field($field)->get($anchor_id);

        if (empty($anchor_info)) {
            return parent::returnData(1, '主播信息不存在');
        }

        if ($anchor_info->is_anchor != 1) {
            return parent::returnData(1, '该用户还不是主播');
        }
        $params = [
            'id'           => $anchor_info->id,
            'nick_name'    => $anchor_info->nick_name,
            'avatar'       => $anchor_info->avatar,
            'is_anchor'    => 1,
            'vip_date'     => '',
            'vip_level'    => 0,
            'user_level'   => self::getUserLevel($anchor_info->point),
            'anchor_level' => self::getAnchorLevel($anchor_info->anchor_point),

        ];
        // 获取数据
        $is_res = Esclient::insert(self::$es_anchor, $params['id'], $params);
        if ($is_res) {
            return parent::returnData(0, '操作成功');
        }
        return parent::returnData(1, '操作失败');
    }


    /**
     * 删除主播es数据
     */
    static public function delAnchorEs($anchor_id)
    {
        // 查询是否存在
        $options = [
            'index' => self::$es_anchor,
            'size'  => 1,
            'from'  => 0,
            'where' => [
                ['id', '=', $anchor_id]
            ],
        ];
        $exist   = Esclient::find($options);
        if (empty($exist)) {
            return true;
        }

        $res = Esclient::delete(self::$es_anchor, $anchor_id);
        if (!empty($res)) {
            return true;
        }
        return false;
    }


    /**
     * 获取用户等级
     * @param $point
     */
    static public function getUserLevel($user_point)
    {
        $user_level = 0;
        // 用户等级
        $level_list = UserLevelRuleModel::select();

        foreach ($level_list as $level_info) {
            if ($user_point < 1) {
                $user_level = 0;
                break;
            }
            if ($user_point = $level_info->point) {
                $user_level = $level_info->level;
            }

            foreach ($level_list as $level_two) {
                if ($user_point >= $level_two->point) {
                    $user_level = $level_two->level;
                }

                if ($user_point >= $level_info->point && $user_point < $level_two->point) {
                    $user_level = $level_info->level;
                }
            }
        }
        return $user_level;
    }

    /**
     * 获取主播经验值
     * @param $point
     */
    static public function getAnchorLevel($anchor_point)
    {
        $user_level = 0;
        // 等级
        $level_list = AnchorLevelRuleModel::select();

        foreach ($level_list as $level_info) {
            if ($anchor_point < 1) {
                $user_level = 0;
                break;
            }
            if ($anchor_point = $level_info->point) {
                $user_level = $level_info->level;
            }

            foreach ($level_list as $level_two) {
                if ($anchor_point >= $level_two->point) {
                    $user_level = $level_two->level;
                }

                if ($anchor_point >= $level_info->point && $anchor_point < $level_two->point) {
                    $user_level = $level_info->level;
                }
            }
        }
        return $user_level;
    }


}